<template>
	<view class="student-container">
		<view class="head">
			<view class="goback" @click="goback"> < </view>
		    <view class="title"> 学生个人信息 </view>
		</view>
		<studentInfo 
		    :studentInfo="getstudentInfo" 
		    :show="true"
		    :showeditButton="false"
		></studentInfo>
		<wButton
		    class="delete"
		    text="删除学生"
		    @click="deletestudents"
		    bgColor="#e54d42"
		></wButton>
	</view>
</template>

<script>
	import studentInfo from '../../components/student-Info/student-Info.vue';
	import wButton from '../../components/watch-login/watch-button.vue';
	import { deleteStudent } from '../../api/class';
	import { getStudentInfo } from '../../api/student';
	
	export default {
		data() {
			return {
				studentId:'',
				classId:'',
				getstudentInfo:{
					studentId: '2023080117',
					studentName: '张伟',
					gender: 1,
					age: '20',
					phoneNumber: '13812345678',
					familyNumber: '13987654321',
					department: '计算机科学与技术学院'
				},
				// getstudentInfo:{
				// 	studentId: '',
				// 	studentName: '',
				// 	gender: 1,
				// 	age: '',
				// 	phoneNumber: '',
				// 	familyNumber: '',
				// 	department: ''
				// },
			}
		},
		components :{ studentInfo , wButton },
		onLoad(options) {
			if(!options.studentId || !options.classid) return 
			this.studentId = options.studentId
			this.classId = options.classid
			this.getstudentsInfo()
		},
		onShow() {
			const userInfo = uni.getStorageSync('userInfo')
			if (!userInfo) {
				uni.redirectTo({ url: '/pages/login/login' })
			}
		},
		methods: {
			goback(){
				uni.navigateBack()
			},
			
			async getstudentsInfo(){
				uni.showLoading({ title:'加载中...' })
				try{
					const res = await getStudentInfo({id :this.studentId})
					if(res.code === 200){
						// 字段映射转换
						this.getstudentInfo = {
							studentId: res.data.id,
							studentName: res.data.name,
							gender: res.data.gender,
							age: res.data.age,
							phoneNumber: res.data.phoneNumber,
							familyNumber: res.data.contact,
							department: res.data.department
						}
					}
				}catch(err){
					uni.showToast({ title: err.message, icon: 'none' })
				}finally{
					uni.hideLoading()
				}
			},

			async deletestudents(){
				uni.showModal({
					title: '提示',
					content: '确定删除该学生吗？',
					success: async (res) => {
						if (res.confirm) {
							uni.showLoading({ title:'处理中...' })
							try{
								const response = await deleteStudent({
									id: this.studentId,
									classId: this.classId
								})
								if(response.code === 200){
									uni.showToast({ title: '删除成功' })
									uni.navigateBack()
								}
							}catch(err){
								uni.showToast({ title: err.message, icon: 'none' })
							}finally{
								uni.hideLoading()
							}
						}
					}
				})
			}
		}
	}
</script>

<style>
	@import url('./css/studentdetail.css');
</style>